/**
 * 
 * @authors Riche
 * @date    2016-07-05 10:39:22
 * @version 1.1.0
 */



var info=new Vue({
  el:"#app",
  data:{
    scrollbar:{},
    CarouselList:[],
    GoodsTypeList:[],
    userInfo:{},
    NewGoodsList:[],
    exchangeList:[],
    reqOut:false, //判断请求数据是否已完
    isLoading:true,
    pageIndex :1,
    pageSize : PAGESIZE
  },
  created:function(){
      var dtd = $.Deferred();
      var _self = this;
      // 最热商品
      var data = {
          pageSize: _self.pageSize,
          pageIndex:_self.pageIndex,
          typeID:"0",
          title:"",
          sortType:2
      };
      data = Common.jsonEncode(data);
      _self.scrollbar = new scrollBar({
          isLoading: _self.isLoading,
          url: "user_getExchangeGoodsList",
          type:"get",
          data:data
      });
       _self.loadlist();
      
      //轮播图
      _self.getCarouselList();
      //商品分类
      _self.getGoodsTypeList();
      // 用户信息
      _self.getUserInfo();
      // 最新商品
      _self.getNewGoodsList();

      
  },
  compiled:function(){
    var _self = this;
    $(document).scroll(function () {
      var docHeight = $(document).height(),
          winHeight = $(window).height();
      if ((docHeight > winHeight)&&($(document).scrollTop() >= docHeight - winHeight)) {
        if(!_self.reqOut){
          _self.reqOut = true;
          var str = '<img class="loading-img" src="images/loading.gif"></img>';
          myLoad.pullLoad('goodList',str);
          _self.loadlist(function(){}); 
        }
      }
    }); 

  },
  methods:{
    loadlist:function(callback){
      var _self = this;
      _self.scrollbar.load(function(pageIndex,data){
        for(var i = 0; i < data.length; i++){
          _self.exchangeList.push(data[i]);
        }
        _self.reqOut = false;

        //实际数据与请求的数据量比较
        if (data.length >= _self.pageSize) {
            myLoad.pullHide(); //加载后，把效果去掉
        } else {
            _self.reqOut = true;
            myLoad.noDate('再拉下去也没有彩蛋');
            _self.isLoading = false;
        }
        _self.pageIndex = pageIndex;
      },function(isLoading){
        _self.isLoading = isLoading;
        //$(".whether").show();
        myLoad.noDate('再拉下去也没有彩蛋');
        _self.reqOut = true;
      });
    },
    getCarouselList:function(){
      //轮播图
      var _self = this;
      var data = {
          size: 3
          //location: 1
      };
      data = Common.jsonEncode(data);
      Common.ajaxCall({ url: 'look_getCarouselList', datatype: 'json', type: 'post', data: data}, function (data) {
          if(data.error_code==0){
              var obj = data.data;
              _self.CarouselList = obj;
          }else{
              alert(data.msg);
          }
      });
    },
    getGoodsTypeList:function(){
      //商品分类
      var _self = this;
      var data = {
      };
      data = Common.jsonEncode(data);
      Common.ajaxCall({ url: 'user_getGoodsTypeList', datatype: 'json', type: 'post', data: data}, function (data) {
          if(data.error_code==0){
              var obj = data.data;
              _self.GoodsTypeList = obj;
          }else{
              alert(data.msg);
          }
      });
    },
    getUserInfo:function(){
      //用户信息
      var _self = this;
      var data = {
      };
      data = Common.jsonEncode(data);
      Common.ajaxCall({ url: 'user_getUserInfo', datatype: 'json', type: 'get', data: data}, function (data) {
          if(data.error_code==0){
              var obj = data.data;
              _self.userInfo = obj;
          }else{
              alert(data.msg);
          }
      });
    },
    getNewGoodsList:function(){
      //最新商品
      var _self = this;
      var data = {
          pageSize: 6,
          pageIndex:1,
          typeID:0,
          title:"",
          sortType:1
      };
      data = Common.jsonEncode(data);
      Common.ajaxCall({ url: 'user_getExchangeGoodsList', datatype: 'json', type: 'post', data: data}, function (data) {
          if(data.error_code==0){
              var obj = data.data;
              _self.NewGoodsList = obj;
          }else{
              alert(data.msg);
          }
      });
    },
    addComment:function(){
      window.location.href = "textArea.html?id=" + dataID;
    },
    loadPhoto:function(){
        // 比例
      myJS.imgScale('.j_3', '3', '1');
      myJS.imgScale('.j_4', '4', '3');
      myJS.ImgFull('.loadImg','images/liby.png');
    },
    search:function(){
      if (jQuery.trim($("#searchword").val()) == "") {
        alert("请输入关键字");
      }
      else {
          var strKeyword = $("#searchword").val();
          window.location = "goodList.html?title=" + encodeURIComponent(strKeyword);
      }
    }

  },
  watch:{
      "exchangeList": function(){
          this.loadPhoto();
      },
      "NewGoodsList":function(){
        this.loadPhoto();
      },
      "CarouselList":function(){
        var swiper = new Swiper('.full2',{
          pagination: '.full2 .pagination',
          paginationClickable: true,
          autoplay: 4000,
          loop: true,
          autoplayDisableOnInteraction:false
        });
      },
      "GoodsTypeList":function(){
        var swiper1 = new Swiper('.nav', {
            slidesPerView: 'auto'
        });
      }
  }

});



